package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.R;
import com.example.reggie.common.annotation.Log;
import com.example.reggie.entity.User;
import com.example.reggie.service.impl.UserServiceImpl;
import com.example.reggie.utils.ValidateCodeUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /*手机验证码*/
    @PostMapping("/sendMsg")
    public R sendMsg(HttpServletRequest request, @RequestBody User user){

        String phone = user.getPhone();
        log.info("dwasdwasdwa",phone);
        System.out.println("手机号码"+phone);

        String yzm = ValidateCodeUtils.generateValidateCode(6).toString();

        redisTemplate.opsForValue().set(phone,yzm,5, TimeUnit.MINUTES);

        log.info(yzm);

//        request.getSession().setAttribute(phone,yzm);

        return R.success("验证码发送成功");
    }


    /**登录
     * log是我们自定义注解*/
    @Log("登录操作")
    /*@ApiOperation 注解是swagger里的，我们可以使用@ApiOperation注解来描述我们写的接口，该注解提供了很多属性供我们来描述接口信息*/
    @ApiOperation("登录")
    @ApiImplicitParams({
/*   1. name ：参数名。
   2. value ： 参数的具体意义，作用。
   3. required ： 参数是否必填。
   4. dataType ：参数的数据类型。
   5. paramType ：查询参数类型，这里有几种形式：
        path 以地址的形式提交数据
        query 直接跟参数完成自动映射赋值
        body 以流的形式提交 仅支持POST
        header 参数在request headers 里边提交
        form 以form表单的形式提交 仅支持POST*/
            @ApiImplicitParam(name = "employee")
    })
    @PostMapping("/login")
    public R login(@RequestBody User user,HttpServletRequest request){

        /*String yzm = (String)request.getSession().getAttribute(user.getPhone());
        System.out.println("s中的"+yzm);*/
        String yzm = (String) redisTemplate.opsForValue().get(user.getPhone());


        if (user.getCode().equals(yzm)){

            /*判断对方是否有账户*/
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone,user.getPhone());
            User one = userService.getOne(wrapper);
            if (one==null){
                userService.save(user);
            }
            LambdaQueryWrapper<User> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(User::getPhone,user.getPhone());
            User one1 = userService.getOne(wrapper1);

            request.getSession().setAttribute("user",one1.getId());

            redisTemplate.delete(user.getPhone());

            return R.success("登录成功");

        }else {
            return R.error("验证码错误,请重新输入");
        }
    }

        @PostMapping("/loginout")
    public R loginout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

}
